package com.raisecom.redis;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.raisecom.util.PropertiesUtil;

/**
 * JResis连接池工具类
 * @author root
 *
 */
public class JRedisPoolUtil {
	private static Logger log = Logger.getLogger(JRedisPoolUtil.class);

	private static JedisPool pool = null;

	public static Jedis getJedisPool() {
		if (pool == null) {
			String host = PropertiesUtil.getKeyString("redis.properties", "redis.host");
			int port = PropertiesUtil.getKeyInteger("redis.properties", "redis.port");
//			int maxactive = PropertiesUtil.getKeyInteger("redis.properties", "redis.max.active");
			int maxwaite = PropertiesUtil.getKeyInteger("redis.properties", "redis.max.wait");
//			int maxidel = PropertiesUtil.getKeyInteger("redis.properties", "redis.max.idel");
			int timeout = PropertiesUtil.getKeyInteger("redis.properties", "redis.timeout");
			JedisPoolConfig jpc = new JedisPoolConfig();
			// jpc.setMaxActive(maxactive);
			jpc.setMaxIdle(maxwaite);
			// jpc.setMaxWait(maxidel);
			jpc.setTestOnBorrow(true);
			String auth = "";
			try {
				auth = PropertiesUtil.getKeyString("redis.properties", "redis.auth");
			} catch (Exception e) {
				log.warn("没有授权信息");
			}
			if (auth != null && !"".equals(auth)) {
				pool = new JedisPool(jpc, host, port, timeout, auth);
			} else {
				pool = new JedisPool(jpc, host, port, timeout);
			}
		}

		return pool.getResource();
	}

	public static void returnJedisPool(Jedis t) {
		pool.returnResource(t);
	}

}
